﻿using AutoMapper;
using KJSyncXP.Extensions;
using KJSyncXP.Services;
using KJSyncXP.Sqlite.Model;
using KJSyncXP.SQLite.Context;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

namespace KJSyncXP.ViewModels
{
    public partial class UserManagerVM :BaseViewModel
    {

        AuthorizationService _authorizationService;

        private AppConfig _appConfig;

        private SqliteDbContext _sqliteCtx;

        private ObservableCollection<User> userList;
        public ObservableCollection<User> UserList
        {
            get => userList;
            set {
                userList = value;
                RaisePropertyChanged("UserList");        
            }
        }

        User _currentUser;
        public User CurrentUser
        {
            get => _currentUser;
            set
            {
                if (_currentUser == value) return;
                _currentUser = value;
            }
        }

        Permission _currentPermission;
        public Permission CurrentPermission
        {
            get => _currentPermission;
            set
            {
                if (_currentPermission == value) return;
                _currentPermission = value;
            }
        }


        private ObservableCollection<Permission> permissionList;
        public ObservableCollection<Permission> PermissionList
        {
            get => permissionList;
            set {
                permissionList = value;
                RaisePropertyChanged("PermissionList");
            }
        }

        private ObservableCollection<Up> upList;

        public ObservableCollection<Up> UpList
        {
            get => upList;
            set
            {
                upList = value;
                RaisePropertyChanged("UpList");
            }
        }

        public UserManagerVM(AuthorizationService authorizationService,AppConfig appConfig, SqliteDbContext sqliteCtx)
        {
            _appConfig = appConfig;
            _authorizationService = authorizationService;
            _sqliteCtx = sqliteCtx;
        }



        /// <summary>
        /// 初始用户的权限。
        /// </summary>
        public void init()
        {
            if (CurrentUser != null)
            {
                this.UpList = _authorizationService.listUp(CurrentUser.Useruuid);
            }
            this.PermissionList = _authorizationService.listPermission();
            var deptname = "A";
            UserList = _authorizationService.listUser(deptname);
            //GrowlUtil.ShowInfo(UserList.Count + "");
        }

        public void saveUser()
        {
            _authorizationService.SaveUser(CurrentUser);
        }

        public void AddUp(string pname, string puuid,string ptype)
        {
            if (_currentUser != null)
            {
                var total = UpList?.Where(x => x.Useruuid == _currentUser.Useruuid && x.Puuid == puuid).Count();
                if (total > 0)
                {
                    //GrowlUtil.ShowInfo("此权限已存在！");
                    return;
                }
                _authorizationService.AddUp(pname, puuid, ptype,_currentUser.Useruuid, _currentUser.Username);
            }
            else
            {
                //GrowlUtil.ShowInfo("请指定要为其添加权限的用户！");
            }
        }


        /// <summary>
        /// 刷新用户所对应的权限清单。
        /// 
        /// </summary>
        /// <param name="useruuid">用户标识</param>
        public void refreshUp()
        {
            if (_currentUser != null)
            {
                this.UpList = _authorizationService.listUp(this.CurrentUser.Useruuid);
            }
        }


        public void newUser()
        {
            CurrentUser = new User()
            {
                Inuse = true,
                State = 0,
                Useruuid = Guid.NewGuid().ToString("N"),
                Userpwd = "",
            };
        }

        public void removeUp(string puuid)
        {
            if (_currentUser != null)
            {
                _authorizationService.RemoveUp(puuid, _currentUser.Useruuid);
            }
        }


        public void removeUser()
        {
            if (CurrentUser != null)
            {
                _authorizationService.RemoveUser(CurrentUser);
            }
        }


        /*

       public List<string> Tokens
       { get; set; } = new List<string>();
       public User CurrentUser
       {
           get => _currentUser;
           set
           {
               if (_currentUser == value) return;
               _currentUser = value;
           }
       }

        */



    }
}
